import json
import time

from sanic import Sanic
from sanic.response import json as respJson

app = Sanic(name="test")
app.config.ACCESS_LOG = False
tinydict = {}


@app.route("/xxs/cp/g")
async def get(request):
    storeParam = str(request.args['store'][0])
    content = tinydict.get(storeParam)
    if content is None:
        return respJson({"content": None, "time": None})
    return respJson(content)


@app.route("/xxs/cp/s", methods=['POST'])
async def save(request):
    requestBody = json.loads(request.body)
    storeParam = str(requestBody.get('store'))
    contentParam = str(requestBody.get('content'))
    tinydict[storeParam] = {"content": contentParam, 'time': time.time()}
    print("save:" + storeParam + ":" + contentParam)
    return respJson({"data": "true"})


if __name__ == "__main__":
    app.run(host="0.0.0.0", port=6377)
